package com.gl.servlet.listener;

import java.io.PrintStream;
import java.util.Date;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnLineUserListener
  implements HttpSessionListener
{
  private static int activeCount = 0;

  public void sessionCreated(HttpSessionEvent evt) {
    activeCount += 1;
    long times = evt.getSession().getCreationTime();
    System.out.println("A new session was created at " + new Date(times) + ";id:" + evt.getSession().getId() + ";MaxInactiveInterval:" + evt.getSession().getMaxInactiveInterval() + ";active count is " + activeCount);
  }

  public void sessionDestroyed(HttpSessionEvent evt)
  {
    activeCount -= 1;
    long times = System.currentTimeMillis();
    System.out.println("a session was destroyed at " + new Date(times) + ";id:" + evt.getSession().getId() + ";active count is " + activeCount);
  }

  public static int getActiveCount()
  {
    return activeCount;
  }
}